//
//  ST_DTChatToolBar.swift
//  mewpoo
//
//  Created by DouLei on 2018/11/27.
//  Copyright © 2018 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit



class ST_DTChatToolBar: UIView {

    var itemFunS = [String]()
    
    var message:ST_DTChatMessage?
    
    var indexPath:IndexPath
    
    init(frame:CGRect,message:ST_DTChatMessage,longPressedRow:IndexPath,triangleViewX:CGFloat) {
        
        self.indexPath = longPressedRow
        
        super.init(frame: frame)
        
        self.message = message
        
        var recall = false
        
        let interval = Int64(Date().timeIntervalSince1970) - message.messageTime
        
        if interval <= 60 * 2 && message.direction == .send{
            recall = true
        }
        
        var copy = false
        
        if message.cellStyle == .Text{
            copy = true
        }
        
        if copy {
            itemFunS.append("复制")
        }
        
        if recall {
            itemFunS.append("撤回")
        }
        
        itemFunS.append("删除")
        
        self.addSubview(backView)
        self.addSubview(triangle_view)
        
        
        let tFrame = CGRect.init(x: triangleViewX - ST_DTChatToolBar.triangle_height/2.0, y: ST_DTChatToolBar.back_view_height-0.5, width: ST_DTChatToolBar.triangle_width, height: ST_DTChatToolBar.triangle_height)
        
        
        var backFrame = CGRect.init(x: triangleViewX - self.back_view_width/2.0, y: 0, width: self.back_view_width, height: ST_DTChatToolBar.back_view_height)
        
        if backFrame.minX < 30 {
            backFrame = CGRect.init(x: 30, y: backFrame.origin.y, width: backFrame.width, height: backFrame.height)
        }
        
        if (backFrame.maxX + 30) > ScreenWidth {
            backFrame = CGRect.init(x: (ScreenWidth-30-backFrame.width), y: backFrame.origin.y, width: backFrame.width, height: backFrame.height)
        }
        
        
        triangle_view.frame = tFrame
        
        backView.frame = backFrame
        
        var line:UIView?
        
        for (idx,item) in itemFunS.enumerated(){
            
            let btn = UIButton.init(type: UIButton.ButtonType.custom)
            btn.setTitleForAllStates(item)
            btn.setTitleColorForAllStates(Color.white)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            btn.titleLabel?.textAlignment = .center
            btn.tag = idx + self.baseTage
            btn.add_touch_up(insideTarget: self, action: #selector(handler(sender:)))
            backView.addSubview(btn)
            
            btn.snp.makeConstraints { (make) in
                if let last = line{
                    make.left.equalTo(last.snp.right)
                }else{
                    make.left.equalTo(self.backView)
                }
                make.top.bottom.equalTo(self.backView)
                make.width.equalTo(self.itemWidth)
            }
            
            if (idx + 1) != itemFunS.count{
                let v = UIView()
                v.backgroundColor = Color.init(white: 1, alpha: 0.3)
                backView.addSubview(v)
                v.snp.makeConstraints { (make) in
                    make.left.equalTo(btn.snp.right)
                    make.height.equalTo(12)
                    make.centerY.equalTo(self.backView)
                    make.width.equalTo(0.5)
                }
                line = v
            }
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //    MARK:- Closure
    var duplicate_text_clousure:(()->())?
    
    var recall_msg_clousure:((String)->())?
    
    var delete_msg_clousure:((String)->())?
    
    
    //    MARK:- Event
    @objc func handler(sender:UIButton){
        
        let idx = sender.tag - self.baseTage
            
            let text = itemFunS[idx]
        
            if text == "复制"{
                
                UIPasteboard.general.string = message?.chatText
                
//                vc.showToast(message: "复制成功")
//                vc.dismissMessageToolBar()
                
                duplicate_text_clousure?()
                
            }
            
            if text == "撤回"{
                
                show_action_sheet(text: text) {
                    self.recall_msg_clousure?(self.message?.uuid ?? "")
                }
                
            }
            
            if text == "删除"{
                
                show_action_sheet(text: text) {
                    self.delete_msg_clousure?(self.message?.uuid ?? "")
                }
                
            }
    }
    
    
    func show_action_sheet(text:String,success:@escaping (()->())){
        
        let messageText = String.init(format: "你确定要%@吗？", text)
        
        let alert = UIAlertController.init(title: nil, message: messageText, preferredStyle: UIAlertController.Style.actionSheet)
        
        alert.addAction(title: "确定", style: UIAlertAction.Style.destructive, isEnabled: true) { (action) in
            
            success()
        }
        
        alert.addAction(title: "取消", style: UIAlertAction.Style.cancel, isEnabled: true) { (action) in
        }
        
        self.parentViewController?.present(alert, animated: true, completion: nil)
    }
    
    
    //    MARK:- Properties
    static let back_view_height:CGFloat = 74/2.0
    
    static let triangle_width:CGFloat = 10
    
    static let triangle_height:CGFloat = 6
    
    lazy var itemWidth:CGFloat = {
        return 45
    }()
    
    lazy var back_view_width:CGFloat = {
        return itemWidth * itemFunS.count.cgFloat + itemFunS.count.cgFloat
    }()
    
    
    
    let baseTage = 4567
    
    
    
    lazy var backView:UIView = {
        let control = UIView()
        control.backgroundColor = MainColor
        control.cornerRadius = 6
        return control
    }()
    
    let MainColor = Color.init(hex: 0x8290A5)

    lazy var triangle_view:UIImageView = {
        let control = UIImageView()
        control.image = UIImage()
//        control.backgroundColor = MainColor
        return control
    }()
    
}


